package cn.udrm.elasticsearch.fileread;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 读取word文件内容
 * @author xurongbei
 */
public class WordReadContent {

    public static String getDocContent(File file) throws IOException {
        FileInputStream fis=new FileInputStream(file);
        /*WordExtractor wordExtractor = new WordExtractor(fis);
        String[] texts=wordExtractor.getParagraphText();
        return StringUtils.join(texts);*/
       HWPFDocument doc = new HWPFDocument(fis);
        Range rang = doc.getRange();
        fis.close();
        return rang.text();
    }

    public static String getDocxContent(File file) throws Exception {
        OPCPackage opcPackage = POIXMLDocument.openPackage(file.getPath());
        POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
        String content=extractor.getText();
        extractor.close();
        return content;
    }
}
